//
//  OrderCell.m
//  YYPHSpeedJump
//
//  Created by sujiang on 15/12/22.
//  Copyright © 2015年 sujiang. All rights reserved.
//

#import "ShareCell.h"

@interface ShareCell ()
{
    NSInteger time;
    NSTimer *timer;
    
}

@property (nonatomic, strong) NSString *remain;
@end


@implementation ShareCell

- (void)awakeFromNib {
    // Initialization code
    self.leftTimeLb.hidden = YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (void)setModel:(SendModel *)model
{
    self.orderNumLb.text = model.deal_id;
    self.titleLb.text = model.name;
    [self.goodsPic sd_setImageWithURL:[NSURL URLWithString:model.head_img_url] placeholderImage:[UIImage imageNamed:@"tu-xiao"]];
    self.currentPriceLb.text = [NSString stringWithFormat:@"￥%@", model.buy_price];
    if (model.status.integerValue == 1){
        [self.shaiBtn setTitle:@"立即付款" forState:(UIControlStateNormal)];
        self.leftTimeLb.hidden = NO;
        self.leftTimeLb.text = [NSString stringWithFormat:@"%@s", model.remain_seconds];
        self.resultLb.text = @"等待支付";
        if (model.remain_wait_seconds.integerValue > 0) {
            time = [model.remain_wait_seconds integerValue] + 60;
        } else {
            time = [model.remain_wait_seconds integerValue];
        }
        [NSThread detachNewThreadSelector:@selector(actionTimeChange) toTarget:self withObject:nil];
        } else if (model.status.integerValue == 3) {
            [self.shaiBtn setTitle:@"提醒发货" forState:(UIControlStateNormal)];
            self.resultLb.text = @"支付成功";
        } else if (model.status.integerValue == 4){
            [self.shaiBtn setTitle:@"确认收货" forState:(UIControlStateNormal)];
            self.resultLb.text = @"支付成功";
        } else if (model.status.integerValue == 5) {
            [self.shaiBtn setTitle:@"晒!" forState:(UIControlStateNormal)];
        } else if (model.status.integerValue == 6) {
            [self.shaiBtn setTitle:@"已晒!" forState:(UIControlStateNormal)];
            [self.shaiBtn setTitleColor:kColors(102, 102, 102, 1) forState:(UIControlStateNormal)];
            self.shaiBtn.userInteractionEnabled = NO;
        } else if (model.status.integerValue == 12) {
            self.resultLb.text = @"订单已完结";
            [self.shaiBtn setTitle:@"订单已完结" forState:(UIControlStateNormal)];
            
            self.shaiBtn.userInteractionEnabled = NO;
            self.deleteBtn.userInteractionEnabled = NO;
        } else {
            [self.shaiBtn setTitle:@"再次购买" forState:(UIControlStateNormal)];
            self.resultLb.text = @"支付失败";
            [self.deleteBtn setTitle:@"删除" forState:(UIControlStateNormal)];
        }
}


- (void)actionTimeChange
{
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(actionSubTime) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] run];
}

- (void)setRemain:(NSString *)remain
{
    _remain = remain;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (remain.integerValue >= 60) {
            self.leftTimeLb.text = @"60s";
            [self.shaiBtn setTitle:[NSString stringWithFormat:@"%lds", remain.integerValue-60] forState:(UIControlStateNormal)];
            [self.shaiBtn setTitleColor:kColors(102, 102, 102, 1) forState:(UIControlStateNormal)];
            self.shaiBtn.userInteractionEnabled = NO;
        } else {
            self.leftTimeLb.text = [NSString stringWithFormat:@"%@s", remain];
            [self.shaiBtn setTitle:@"立即支付" forState:(UIControlStateNormal)];
            [self.shaiBtn setTitleColor:kColors(218, 166, 46, 1) forState:(UIControlStateNormal)];
            self.shaiBtn.userInteractionEnabled = YES;
        }
    });
}

- (void)actionSubTime
{
    if (time <= 0) {
        [timer invalidate];
        timer = nil;
        return;
    }
    time -= 1;
    self.remain = [NSString stringWithFormat:@"%ld", time];
    
}


@end
